package com.isoft.student.service.impl;

import com.isoft.student.dao.AnswerMapper;
import com.isoft.student.dao.AnswerspaperMapper;
import com.isoft.student.eo.AnswerspaperEOPage;
import com.isoft.student.model.Answerspaper;
import com.isoft.student.service.AnswerspaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 实现业务及流程接口当中的内容
 */
@Service
public class AnswerspaperServiceImpl implements AnswerspaperService {

    @Autowired
    private AnswerspaperMapper answerspaperMapper;
    @Autowired
    private AnswerMapper answerMapper;

    /**
     * 试卷答题表添加方法实现
     * @param answerspaper 试卷答题
     * @return 是否成功（-1 重复添加）
     */
    @Override
    public int AddAnswerspaper(Answerspaper answerspaper) {
        int returnInt=0;
        //业务判断
        //（1）答题名称不能重复

        //(1)判断答题
        int ExampaperidCount=answerspaperMapper.getExampaperidCount(answerspaper.getExampaperid());
        if (ExampaperidCount<1){
            //2.答题信息数据添加
            int addExampaperidInt=answerspaperMapper.insertAnswerspaper(answerspaper);
            //3.返回值设置
            returnInt=addExampaperidInt;
        }else {
            returnInt=-1;//有相同的信息名称
        }
        return returnInt;
    }

    /**
     * 试卷答题更新
     * @param answerspaper 试卷答题
     * @return 更新是否成功（-1：更新执行SQL报错）
     */
    @Override
    public int UpdateAnswerspaper(Answerspaper answerspaper) {
        int isUpdate=0;
        //1.没有相关业务流程的判断
        try {
            isUpdate=answerspaperMapper.updateAnswerspaper(answerspaper);
        }catch (Exception e){
            isUpdate=-1;
            //打印更新报错的情况
            System.out.print("更新信息时执行SQL报错的原因为："+e.getMessage());
        }
        return isUpdate;
    }

    /**
     * 试卷答题表
     * @param answerid
     * @return
     */
    @Override
    public int DeleteAnswerspaper(int answerid) {
       int delAnswerspaper=0;
        /*1.有关业务流程的判断
         * （1）删除试卷答题不能与答题表有关联
         * */
        try {
            //返回的结果总数量小于1，才可以删除
            if (answerMapper.getAnswerspaperCount(answerid)>0){
                delAnswerspaper=answerspaperMapper.delAnswerspaper(answerid);
            }
        }catch (Exception e){
            delAnswerspaper=-1;
            System.out.print("删除角色信息室执行SQL时报错，信息为："+e.getMessage());
        }
        return delAnswerspaper;
    }

    @Override
    public List<AnswerspaperEOPage> getAnswerspaperWhereAll(AnswerspaperEOPage answerspaperEOPage) {
        List<AnswerspaperEOPage> listAnswerspaperEo=answerspaperMapper.getAnswerspaperWhereAll(answerspaperEOPage);
        return listAnswerspaperEo;
    }
}
